<template>
  <div class="login">
    <el-card>
      <div class="login-title">简书后台管理系统</div>
      <el-form :model="form">
        <el-form-item>
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.pwd" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClickButtonSignIn">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  import {mapActions} from 'vuex'

  export default {
    data() {
      return {
        form: {
          username: '',
          pwd: ''
        }
      }
    },
    methods: {
      ...mapActions(['setUser']),
      handleClickButtonSignIn() {
        this.$http({
          path: 'http://localhost:3000/users/login',
          method: 'post',
          params: {
            username: this.form.username,
            pwd: this.form.pwd
          }
        }).then(rel => {
          const {code, msg, token, user} = rel.data
          if (code === 200) {
            this.$message({
              message: msg,
              type: 'success'
            })
            user.token = token
            this.setUser(user)
            this.$router.push({name: 'Index'})
          } else {
            this.$message({
              message: msg,
              type: 'error'
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .login {
    width: 100%;
    height: 100%;
    background-color: #00B7FF;
    display: flex;
    justify-content: center;
    align-items: center;

    .el-card {
      width: 420px;
      height: 300px;
    }

    .login-title {
      font-size: 24px;
      font-weight: bold;
      text-align: center;
      margin-bottom: 50px;
      color: #333;
    }
  }

</style>
